import java.util.Arrays;
import java.util.Scanner;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1,3,4,7,9,11,12,16,21};
        int left = 0,right = arr.length-1;
        int mid ,key;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要搜索的值：");
        key = scanner.nextInt();
        while(left < right){
            mid = (left + right) / 2;
            if(arr[mid] == key){
                System.out.println("查到了，下标是"+mid);
                break;
            }else if(arr[mid] < key){
                left = mid+1;
            }else{
                right = mid -1;
            }
        }
        if(left >= right){
            System.out.println("数组中没有"+key);
            System.out.println(Arrays.toString(arr));;
        }
    }
}
